package com.ddc.study.feature.collect;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.ddc.study.R;
import com.ddc.study.base.BaseActivity;
import com.ddc.study.feature.collect.article.CollectArticleFragment;
import com.ddc.study.feature.collect.website.CollectWebsiteFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/**
 * Created on 2018/5/14.
 *
 * @author lshun
 * @version 1.0
 */
public class CollectActivity extends BaseActivity {
    @BindView(R.id.tab)
    TabLayout mTabLayout;

    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Nullable
    public static List<Integer> getUnCollectIdList(@Nullable Intent intent) {
        return CollectArticleFragment.getUnCollectIdList(intent);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_collection;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return CollectArticleFragment.getInstance();
                    case 1:
                        return CollectWebsiteFragment.getInstance();
                }
                return new Fragment();//return new empty fragment
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return getString(R.string.article);
                    case 1:
                        return getString(R.string.website);
                }
                return null;
            }
        });

        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}
